<html>
<head><meta charset="utf-8"><title>no item granting read acces to tag · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html">no item granting read acces to tag</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="189524928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189524928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189524928">(Mar 02 2020 at 19:11)</a>:</h4>
<p>Hey All,</p>
<p>I'm writing some code which requires the use of unsafe and I'm trying to wrap my head around the miri error <code>no item granting read access to tag</code> What I'm doing is creating an array of 32bit ints and then getting a u8 pointer to that same array. The issue I'm hitting is that I create the array and pointer as</p>
<div class="codehilite"><pre><span></span>    let mut state: [u32; 12] = [0; 12];
    let state_8 = unsafe {
        std::slice::from_raw_parts_mut(
            state.as_mut_ptr() as *mut u8,
            48)
    };
</pre></div>


<p>Then go into a loop of the form</p>
<div class="codehilite"><pre><span></span>while input_byte_len &gt; 0 {
        block_size = min(input_byte_len, 16);
        for i in 0..block_size {
            let b = input.next().unwrap().expect(&quot;Read error on input&quot;);
            state_8[i as usize] ^= b;
        }
        ...
    }
</pre></div>


<p>The line that gets flagged is <code>state_8[i as usize] ^= b;</code>. I've tried to make a minimal example of this which can be seen here<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=589490937b36480799043d790f5d0d4e" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=589490937b36480799043d790f5d0d4e">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=589490937b36480799043d790f5d0d4e</a><br>
but it passes <code>cargo miri test</code> without issue. Also, I've tried moving the pointer creation into the loop and that seems to resolve the issue. <br>
ex.</p>
<div class="codehilite"><pre><span></span>while input_byte_len &gt; 0 {
        block_size = min(input_byte_len, 16);
        for i in 0..block_size {
        let state_8 = unsafe {std::slice::from_raw_parts_mut(state.as_mut_ptr() as *mut u8,48)};
            let b = input.next().unwrap().expect(&quot;Read error on input&quot;);
            state_8[i as usize] ^= b;
        }
        ...
    }
</pre></div>


<p>I'm not really sure where to go from here and could really use some guidance. For context my full set of code with branch adding a miri CI check can be found here<br>
<a href="https://github.com/darakian/gimli/tree/add-miri-ci" target="_blank" title="https://github.com/darakian/gimli/tree/add-miri-ci">https://github.com/darakian/gimli/tree/add-miri-ci</a></p>
<p>The full cli output of the miri error is </p>
<div class="codehilite"><pre><span></span>error[E0080]: Miri evaluation error: no item granting read access to tag &lt;87365&gt; found in borrow stack
   --&gt; src/lib.rs:70:13
    |
70  |             state_8[i as usize] ^= b;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^ Miri evaluation error: no item granting read access to tag &lt;87365&gt; found in borrow stack
    |
</pre></div>



<a name="189529889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189529889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189529889">(Mar 02 2020 at 19:55)</a>:</h4>
<p>Are you sure your version of Miri matches the one in the playground?</p>



<a name="189529990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189529990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189529990">(Mar 02 2020 at 19:56)</a>:</h4>
<p>And have you seen <a href="https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut" target="_blank" title="https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut">https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut</a> ?</p>



<a name="189530459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189530459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189530459">(Mar 02 2020 at 20:01)</a>:</h4>
<p>Your playground has</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">block_size</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>

<span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="n">block_size</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* ... */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>The loop will never run.</p>



<a name="189530577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189530577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189530577">(Mar 02 2020 at 20:03)</a>:</h4>
<p>Oh, that's being strangely mutable, I see.</p>



<a name="189533910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189533910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189533910">(Mar 02 2020 at 20:41)</a>:</h4>
<p>No, it seems that there's a mismatch in versions. <br>
I have <code>miri 0.1.0 (07ac1027 2019-09-29)</code> locally. It seems like the playground is using <code>0.1.0 (2020-02-23 3c444bf)</code>. <br>
In my CI I have <code>miri 0.1.0 (3c444bf 2020-02-23)</code></p>
<p>On the blocksize, ya that number should be 16 for each loop until the input iterator has fewer than 16 bytes in it. The playground initially had <code>block_size = min(input_byte_len, 64);</code> which was a type. I've updated it to <code>block_size = min(input_byte_len, 16);</code>.</p>
<p>The strangest bit of this to me is that by moving the pointer creation into the innermost loop resolves the issue. This makes me think that there's something preventing the inner scope from reading the outerscope, but this is also my understanding at the rust source level rather than at the MIR level.</p>



<a name="189535131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189535131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189535131">(Mar 02 2020 at 20:53)</a>:</h4>
<p>Actually. Is it possible that a scope could be taking ownership of the pointer at the MIR level?</p>



<a name="189535395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189535395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189535395">(Mar 02 2020 at 20:55)</a>:</h4>
<p>What happens if you replace it with the <code>bytemuck</code> crate and use the <code>cast_slice</code> function to do the conversion?</p>



<a name="189536681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189536681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189536681">(Mar 02 2020 at 21:07)</a>:</h4>
<p>Replacing</p>
<div class="codehilite"><pre><span></span>    let state_8 = unsafe {
        std::slice::from_raw_parts_mut(
            state.as_mut_ptr() as *mut u8,
            48)
    };
</pre></div>


<p>with <code>let state_8 = cast_slice_mut(&amp;mut state);</code> which I'm not sure is the correct syntax. My code no longer compiles due to multiple mutable borrows. Using <code>cast_slice</code> I have similar errors but around the <code>state_8</code> variable not being mutable. However, I've never used the <code>bytemuck</code> crate before and I'm not sure if this is correct usage. Their docs leave a lot to be desired.</p>



<a name="189539932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189539932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189539932">(Mar 02 2020 at 21:39)</a>:</h4>
<p>Well, if I understand the rest of your situation correctly then that usage is accurate and your code's borrow errors are the source of the miri failures.</p>



<a name="189540052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189540052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189540052">(Mar 02 2020 at 21:40)</a>:</h4>
<p>However, I apologise for the docs situation! If you have ideas on how to improve them please send in an issue. Or even just send in an issue about the problem without a solution and I'll do my best to fix it up.</p>



<a name="189542890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189542890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189542890">(Mar 02 2020 at 22:06)</a>:</h4>
<p>A simple example would be nice for the docs. My confusion at the moment around the <code>cast_slice(_mut)</code> functions are how <code>U</code> and <code>T</code> are derived. For instance it's unclear to me if <code>let state_8 = cast_slice_mut(&amp;mut state);</code> will return a &amp;[u8] in my case. I'm not sure if that information can be inferred or not.</p>
<p>Anyway, yes my borrow errors most certainly are coming from having two mutable pointers to the same data structure. These two pointers are a requirement at least for now and hence the unsafe usage :)<br>
I suppose this may just be an anti-pattern that miri doesn't deal with, but I was hoping that this would be a case which was covered.</p>
<p>Edit:<br>
Here's a minimal failing playground example<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a903cf056ab3d95514cd28acd4a405fc" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a903cf056ab3d95514cd28acd4a405fc">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a903cf056ab3d95514cd28acd4a405fc</a></p>
<p>It seems like miri only fails when the use of the <code>gimli</code> function is present.<br>
Removing line 75; <code>gimli(&amp;mut state);</code> on the playground above resolves the miri issues.</p>



<a name="189544842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189544842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189544842">(Mar 02 2020 at 22:31)</a>:</h4>
<p>Often the output type of a cast cannot be inferred because rust has kinda weak inference and there's so many possible outputs. It is a mild weakness of the library</p>



<a name="189545095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189545095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189545095">(Mar 02 2020 at 22:34)</a>:</h4>
<p>as to the aliasing issue: raw pointers actually can alias, but unique references cannot</p>



<a name="189545130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189545130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189545130">(Mar 02 2020 at 22:35)</a>:</h4>
<p>so you could maybe to what you want if you never make a &amp;mut T pointing at the stuff</p>



<a name="189545139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189545139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189545139">(Mar 02 2020 at 22:35)</a>:</h4>
<p>but that means a lot of rust can't be used, sadly</p>



<a name="189545383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189545383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189545383">(Mar 02 2020 at 22:39)</a>:</h4>
<p>Sadly the algorithm I'm porting requires two mutable views of the same array. I believe this requires two mutable pointers, but I would be happy to be wrong there.</p>



<a name="189547800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189547800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189547800">(Mar 02 2020 at 23:14)</a>:</h4>
<p>could maybe go by index instead of by pointer</p>



<a name="189549333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189549333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189549333">(Mar 02 2020 at 23:38)</a>:</h4>
<p>Perhaps, I'll have to investigate it. Thanks for the help and for letting me think out loud a bit :)</p>



<a name="189617682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189617682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189617682">(Mar 03 2020 at 18:01)</a>:</h4>
<p><span class="user-mention silent" data-user-id="266754">Yogurt</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189545383" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189545383">said</a>:</p>
<blockquote>
<p>Sadly the algorithm I'm porting requires two mutable views of the same array. I believe this requires two mutable pointers, but I would be happy to be wrong there.</p>
</blockquote>
<p>given that the entire point of mutable referneces is not to alias, this sadly will mean that you have to use raw pointers (or indices) throughout</p>



<a name="189617774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189617774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189617774">(Mar 03 2020 at 18:02)</a>:</h4>
<p>(the code is still a bit long for me to understand the issue at a glance, and it seems y'all got some idea about where the problem is, so I take it its not worth for me to dig into it right now)</p>



<a name="189619404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189619404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189619404">(Mar 03 2020 at 18:20)</a>:</h4>
<p>Also without fully grokking the code, I feel like many algorithms can be restated in a way that doesn't require double mutable references.</p>



<a name="189622534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189622534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189622534">(Mar 03 2020 at 18:55)</a>:</h4>
<p>To <span class="user-mention" data-user-id="116155">@Jake Goulding</span>  point; I think I can rewrite the algorithm to avoid the usage of two pointers. This is on my radar, but that's an exploration down the road.  <span class="user-mention" data-user-id="120791">@RalfJ</span>  Here's an even more minimal example of the issue with a fake function.<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=11f0edd22e7950f23155c6f8e6a2364e" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=11f0edd22e7950f23155c6f8e6a2364e">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=11f0edd22e7950f23155c6f8e6a2364e</a></p>
<p>The crux seems to be that in the loop</p>
<div class="codehilite"><pre><span></span>while input_len &gt; 0 {
...
}
</pre></div>


<p>the call to the <code>gimli</code> function seems to take ownership or otherwise makes referencing <code>state_8[i]</code> invalid. I would love to know the rational there because as I understand it I am simply borrowing the pointer (rather than taking ownership) with the <code>gimli</code> function. Moving the gimli call out of the while loop allows miri to pass without complaint. Similarly if the input vector is under 16 elements long miri passes, so I can only assume that the access error occurs only after the first call to <code>gimli</code>.<br>
ex.<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=34c4abd62f3043495aa421026ce65219" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=34c4abd62f3043495aa421026ce65219">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=34c4abd62f3043495aa421026ce65219</a></p>



<a name="189623363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189623363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189623363">(Mar 03 2020 at 19:04)</a>:</h4>
<p><code>state</code> and <code>state8</code> alias each other, don't they?</p>



<a name="189623409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189623409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189623409">(Mar 03 2020 at 19:05)</a>:</h4>
<p>So when you call <code>gimli</code> passing in <code>state</code>, <code>state8</code> needs to be invalidated</p>



<a name="189623576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189623576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189623576">(Mar 03 2020 at 19:07)</a>:</h4>
<p>e.g. I think this is the same:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">state</span>: <span class="p">[</span><span class="kt">u32</span><span class="p">;</span><span class="w"> </span><span class="mi">12</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">12</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">state_8</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">slice</span>::<span class="n">from_raw_parts_mut</span><span class="p">(</span><span class="n">state</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="mi">48</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">_i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="mi">2</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">state_8</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">state</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="189623710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189623710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189623710">(Mar 03 2020 at 19:08)</a>:</h4>
<p>Which is why putting the call to <code>from_raw_parts</code> inside the loop fixes it — you are retaking the single mutable reference</p>



<a name="189624036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189624036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189624036">(Mar 03 2020 at 19:12)</a>:</h4>
<p>Yes that's my understanding as well, but I suppose I don't understand why.  If the state8 pointer needs to be invalidated doesn't that somewhat defeat the point of having two mutable references?</p>



<a name="189624245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189624245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189624245">(Mar 03 2020 at 19:14)</a>:</h4>
<p>Or simpler</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">v</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">v</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">a</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="c1">// let _z = *b; // Causes Miri failure</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="189625077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189625077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189625077">(Mar 03 2020 at 19:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="266754">Yogurt</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189624036" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189624036">said</a>:</p>
<blockquote>
<p>Yes that's my understanding as well, but I suppose I don't understand why.  If the state8 pointer needs to be invalidated doesn't that somewhat defeat the point of having two mutable references?</p>
</blockquote>
<p>well you are not supposed to have two mutable references, that is the entire point. mutable references must be unique, only one can point to any memory at any given point in time (and be currently "usable").</p>



<a name="189625137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189625137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189625137">(Mar 03 2020 at 19:24)</a>:</h4>
<p>IOW, "you can have aliasing or mutation, but never both at the same time" -- the fundamental principle of ownership and borrowing in Rust</p>



<a name="189627861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189627861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189627861">(Mar 03 2020 at 19:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189625137" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189625137">said</a>:</p>
<blockquote>
<p>IOW, "you can have aliasing or mutation, but never both at the same time" -- the fundamental principle of ownership and borrowing in Rust</p>
</blockquote>
<p>I thought this is a fundamental principle of safe rust though. I understand that I am breaking that principle with my use of unsafe and as such I'm trying to add Miri to my workflow so that I can have a better assurance of my usage of unsafe. If that's not a use case for miri then I'm simply out of luck until I can refactor my pointer usage. Thanks again for the help :)</p>



<a name="189628360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189628360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189628360">(Mar 03 2020 at 19:59)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> another way of approaching this problem is "I want to interleave treating <code>state</code> as <code>&amp;mut [u32]</code> and <code>&amp;mut [u8]</code>, but never both at the same time". </p>
<p>Is there some way of doing some kind of union or reborrowing here?</p>



<a name="189628893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189628893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189628893">(Mar 03 2020 at 20:04)</a>:</h4>
<p>this is a fundamental principle of <em>all of</em> Rust, not just safe Rust. I <a href="https://www.youtube.com/watch?v=h9Fh4jRDGLo" target="_blank" title="https://www.youtube.com/watch?v=h9Fh4jRDGLo">recently gave a talk</a> explaining why. :)<br>
only raw pointers (and to a lesser extend <code>UnsafeCell</code>) are allowed to get around this, and alias each other during mutation. So indeed you need unsafe code to get around this, but just adding unsafe is not enough -- the reason you need unsafe is that you need raw pointers to get around the principle, and raw pointers only work in unsafe code.</p>
<div class="youtube-video message_inline_image"><a data-id="h9Fh4jRDGLo" href="https://www.youtube.com/watch?v=h9Fh4jRDGLo" target="_blank" title="https://www.youtube.com/watch?v=h9Fh4jRDGLo"><img src="https://i.ytimg.com/vi/h9Fh4jRDGLo/default.jpg"></a></div>



<a name="189629192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629192">(Mar 03 2020 at 20:07)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> you cannot interleave the use of two different mutable refs, no exceptions. That is the entire point of Stacked Borrows.^^<br>
What you could be able to do is construct <code>&amp;[Cell&lt;u32&gt;]</code> and <code>&amp;[Cell&lt;u8&gt;]</code> appropriately and interleave those (<a href="https://doc.rust-lang.org/nightly/std/cell/struct.Cell.html#method.from_mut" target="_blank" title="https://doc.rust-lang.org/nightly/std/cell/struct.Cell.html#method.from_mut"><code>from_mut</code></a> is useful here).</p>



<a name="189629310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629310">(Mar 03 2020 at 20:08)</a>:</h4>
<p>You can make the sugar sweeter and do the transforms more transparently, but still Miri-clean: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4790f42b00be9e979cc9bd394eaa3d3b" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4790f42b00be9e979cc9bd394eaa3d3b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4790f42b00be9e979cc9bd394eaa3d3b</a></p>



<a name="189629403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629403">(Mar 03 2020 at 20:09)</a>:</h4>
<p>I wonder how well <code>from_parts</code> optimizes away.</p>



<a name="189629428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629428">(Mar 03 2020 at 20:09)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  well, it's a stack, right? Just add a method that does <code>stack::swap_top(N)</code> <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="189629431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629431">(Mar 03 2020 at 20:09)</a>:</h4>
<p>I mean sure if you re-cosntruct a new slice from the origin reference each time, of course that also works</p>



<a name="189629524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629524">(Mar 03 2020 at 20:10)</a>:</h4>
<p>yes, I'm just showing a way to reconstruct it each time so that that doesn't get in the way of the algorithm. Also why I muse about the optimizer.</p>



<a name="189629548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629548">(Mar 03 2020 at 20:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189629428" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189629428">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span>  well, it's a stack, right? Just add a method that does <code>stack::swap_top(N)</code> <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>
</blockquote>
<p>sure we could do that, and then we'll just not be able to do any of the optimizations that motivate SB in the first place ;)</p>



<a name="189629565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629565">(Mar 03 2020 at 20:11)</a>:</h4>
<p><code>stack::shuffle</code></p>



<a name="189629583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629583">(Mar 03 2020 at 20:11)</a>:</h4>
<p>For my case I certainly can reconstruct the reference each time. It's a bit ugly and my C friends will probably cock their eyebrows at me, but <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="189629684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629684">(Mar 03 2020 at 20:12)</a>:</h4>
<p>the alternative if you want to use references "on the inside" is to work with interior mutabulity -- i.e, <code>Cell</code>. those types "know" that there can be aliasing so optimziations are suspended around them.</p>



<a name="189629725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629725">(Mar 03 2020 at 20:13)</a>:</h4>
<p>well the end goal is to get away from aliasing so.... I'll avoid that, but thanks :)</p>



<a name="189629756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629756">(Mar 03 2020 at 20:13)</a>:</h4>
<p>your original code had aliasing though, that is the entire problem ;) and the C version of this has aliasing, too.</p>



<a name="189629761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629761">(Mar 03 2020 at 20:13)</a>:</h4>
<p>one way or another, you need to tell the compiler about the aliasing. I'm afraid that is the price we have to pay if we want the compiler to optimize our code based on aliasing assumptions.</p>



<a name="189629765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629765">(Mar 03 2020 at 20:13)</a>:</h4>
<p><span class="user-mention" data-user-id="266754">@Yogurt</span> that is why I showed the prettier way of reconstructing it</p>



<a name="189629821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629821">(Mar 03 2020 at 20:14)</a>:</h4>
<p>yeah, <span class="user-mention" data-user-id="116155">@Jake Goulding</span> 's approach is the one that avoids aliasing</p>



<a name="189629848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629848">(Mar 03 2020 at 20:14)</a>:</h4>
<p>Also, if you change <code>gimli</code> to take in <code>&amp;[u8]</code> then you'd also succeed, I think</p>



<a name="189629854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629854">(Mar 03 2020 at 20:14)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> wow you didn't even hard-code the factor of <code>4</code> for the size like I would have^^</p>



<a name="189629925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629925">(Mar 03 2020 at 20:15)</a>:</h4>
<p>I do <em>not</em> believe in my ability to do basic math like that always</p>



<a name="189629934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629934">(Mar 03 2020 at 20:15)</a>:</h4>
<p>ah but I think there's a mistake... you are using the slice <code>self.0</code> after creating the raw ptr</p>



<a name="189629938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189629938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189629938">(Mar 03 2020 at 20:15)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Yes, but the 8bit pointer is a convenience. The same math can be done with 32bit pointers and masks  </p>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> This code block?</p>
<div class="codehilite"><pre><span></span>fn main() {
    let mut v: i32 = 0;
    let a: &amp;mut i32 = &amp;mut v;
    let b: &amp;mut i32 = unsafe { &amp;mut *(a as *mut _) };

    let _x = *b;
    let _y = *a;
    // let _z = *b; // Causes Miri failure
}
</pre></div>


<p>Isn't that still aliased?</p>



<a name="189630011"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630011" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630011">(Mar 03 2020 at 20:16)</a>:</h4>
<div class="codehilite"><pre><span></span>        unsafe {
            let p = self.0.as_mut_ptr();
            let u8_len = self.0.len() / mem::size_of::&lt;u8&gt;() * mem::size_of::&lt;u32&gt;();
            slice::from_raw_parts_mut(p as *mut u8, u8_len)
        }
</pre></div>


<p>this is exactly like the mistake I recently <a href="https://github.com/rust-lang/rust/pull/69581" target="_blank" title="https://github.com/rust-lang/rust/pull/69581">fixed in <code>align_to_mut</code></a></p>



<a name="189630041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630041">(Mar 03 2020 at 20:16)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> you should take that up with the maintainers of Miri — it says it's alllllll good</p>



<a name="189630097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630097">(Mar 03 2020 at 20:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189630041" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189630041">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> you should take that up with the maintainers of Miri — it says it's alllllll good</p>
</blockquote>
<p>yeah I am wondering why that is</p>



<a name="189630117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630117">(Mar 03 2020 at 20:17)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> After creating the raw pointer, but before creating the second <em>reference</em> — now just having the raw pointer is an issue?</p>



<a name="189630176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630176">(Mar 03 2020 at 20:18)</a>:</h4>
<p><span class="user-mention" data-user-id="266754">@Yogurt</span> your code block without <code>_z</code> can be written in safe code. this is the kind of "well-nested" aliasing that rust permits -- only one reference being "active" at any point in time</p>



<a name="189630212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630212">(Mar 03 2020 at 20:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189630117" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189630117">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> After creating the raw pointer, but before creating the second <em>reference</em> — now just having the raw pointer is an issue?</p>
</blockquote>
<p>no the issue is that <code>self.0.len()</code> is <code>slice::len(&amp;*self.0)</code>. you are creating a new shared ref.</p>



<a name="189630222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630222">(Mar 03 2020 at 20:18)</a>:</h4>
<p><span class="user-mention" data-user-id="266754">@Yogurt</span> this one specifically - <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4790f42b00be9e979cc9bd394eaa3d3b" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4790f42b00be9e979cc9bd394eaa3d3b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4790f42b00be9e979cc9bd394eaa3d3b</a></p>



<a name="189630229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630229">(Mar 03 2020 at 20:19)</a>:</h4>
<p>ah but unlike in the <code>align_to_mut</code> case you didnt create a mutable ref <em>before</em> that</p>



<a name="189630250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630250">(Mar 03 2020 at 20:19)</a>:</h4>
<p>so you are fine actually, Miri was right^^</p>



<a name="189630278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630278">(Mar 03 2020 at 20:19)</a>:</h4>
<p>right. The ref is the last thing. I'm surprised the align_to_mut wasnt</p>



<a name="189630282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630282">(Mar 03 2020 at 20:19)</a>:</h4>
<p>calling <code>self.0.len()</code> invalidates any mutable refs you might have created since <code>self.0.as_mut_ptr()</code>. which I hope makes sense?</p>



<a name="189630347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630347">(Mar 03 2020 at 20:20)</a>:</h4>
<p>you are creating a shared ref, that invalidates mutable refs derived before as they dont allow aliasing</p>



<a name="189630352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630352">(Mar 03 2020 at 20:20)</a>:</h4>
<p>Oh, because it created the middle / aligned reference first</p>



<a name="189630375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630375">(Mar 03 2020 at 20:20)</a>:</h4>
<p>exactly</p>



<a name="189630424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630424">(Mar 03 2020 at 20:21)</a>:</h4>
<p>The creation was still in (semi-)tail-position, which is where it belongs, just that it was in that tuple.</p>



<a name="189630460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630460">(Mar 03 2020 at 20:21)</a>:</h4>
<p>yeah it was created late but not quite late enough</p>



<a name="189630970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189630970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189630970">(Mar 03 2020 at 20:28)</a>:</h4>
<p>I kinda wanna see what happens with</p>
<div class="codehilite"><pre><span></span><span class="k">union</span> <span class="nc">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">A</span><span class="p">([</span><span class="kt">u8</span><span class="p">]),</span><span class="w"></span>
<span class="w">    </span><span class="n">B</span><span class="p">([</span><span class="kt">u32</span><span class="p">]),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">//convert &amp;mut[u32] -&gt; &amp;mut X</span>
</pre></div>


<p>but I'm lazy.</p>



<a name="189632744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189632744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189632744">(Mar 03 2020 at 20:50)</a>:</h4>
<p>well that certainly wont help avoid unsafe code^^</p>



<a name="189724945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189724945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Darakian <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189724945">(Mar 04 2020 at 19:18)</a>:</h4>
<p>One more question on this thread. When aliasing a piece of data why is it that calling a helper function invalidates the aliased pointer?<br>
ex.<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=55371cda2bcd02fc79e7aa25ff385f79" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=55371cda2bcd02fc79e7aa25ff385f79">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=55371cda2bcd02fc79e7aa25ff385f79</a></p>



<a name="189734620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189734620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189734620">(Mar 04 2020 at 21:03)</a>:</h4>
<p>Just <em>as an approximation</em>, anything that would cause a borrow check error with only references will probably invalidate a pointer when you mix references and pointers. Usually once you start using pointers you should stay with only pointers until you're done with all pointer work, and then go back to using references.</p>
<p>This is part of why bytemuck is useful, because since it's a dedicated function it has lifetime annotations on how the inputs and outputs are linked, so the compiler realizes what's going on.</p>



<a name="189919889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no%20item%20granting%20read%20acces%20to%20tag/near/189919889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag.html#189919889">(Mar 06 2020 at 18:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="266754">Yogurt</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189724945" title="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/no.20item.20granting.20read.20acces.20to.20tag/near/189724945">said</a>:</p>
<blockquote>
<p>One more question on this thread. When aliasing a piece of data why is it that calling a helper function invalidates the aliased pointer?<br>
ex.<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=55371cda2bcd02fc79e7aa25ff385f79" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=55371cda2bcd02fc79e7aa25ff385f79">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=55371cda2bcd02fc79e7aa25ff385f79</a></p>
</blockquote>
<p>to get the optimizations we want, calling that helper function (even though it does nothing) basically has the same effect as actually writing to that memory using the reference passed to the function</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>